var t = getApp();

Page({
    data: {
        imgUrl: "",
        TabCur: 0,
        scrollLeft: 0,
        emojiItems: [ "熊猫头", "蘑菇头", "红脸蛋", "其他" ],
        ad: {},
        imageList:[],
        pageNum:1
    },
    onShow(){
        this.getRandomAd()
        getApp().chaping()
    },
    onReachBottom(){
        let that = this
        that.setData({
            pageNum:that.data.pageNum+1
        })
        that.getList()
    },
    getList(){
        let that = this
        wx.showLoading({
          title: '请稍等...',
        })
        wx.request({
          url: 'https://yezii.haozii.com/wp-json/wp/v2/posts',
          data:{
            per_page: 18,
            categories: 1,
            page: that.data.pageNum
          },
          success(res){
              that.setData({
                  imageList:that.data.imageList.concat(res.data)
              })
            console.log(res)
          },
          complete(){
              wx.hideLoading({
                success: (res) => {},
              })
          }
        })
    },
    onLoad: function() {
        getApp().getRandomAd(true,this)
        this.getRandomAd(true)
        wx.showLoading({
            title: "加载中"
        });
        var o = this;
        this.getList()
        // wx.request({
        //     url: "https://ishare20.net/emotion2/1.json",
        //     header: {
        //         "content-type": "application/json"
        //     },
        //     success: function(t) {
        //         wx.hideLoading(), o.setData({
        //             imgUrl: t.data
        //         });
        //     },
        //     fail: function(t) {
        //         wx.showToast({
        //             title: "连接出错，请稍后重试",
        //             icon: "none",
        //             duration: 2e3
        //         });
        //     }
        // });
    },
    tabSelect: function(t) {
        this.getRandomAd()
        this.setData({
            TabCur: t.currentTarget.dataset.id
        }), wx.showLoading({
            title: "加载中"
        });
        var o = this;
        wx.request({
            url: "https://ishare20.net/emotion2/" + (t.currentTarget.dataset.id + 1) + ".json",
            header: {
                "content-type": "application/json"
            },
            success: function(t) {
                wx.hideLoading(), o.setData({
                    imgUrl: t.data
                });
            },
            fail: function(t) {
                wx.showToast({
                    title: "连接出错，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    chooseFromAlbum: function() {
        let that =  this
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album" ],
            success: async function(t) {
                var o = t.tempFilePaths[0];
                let imgData = await getApp().checkImg(t.tempFilePaths[0],that)
                console.log(imgData)
                if(!imgData||imgData.result.errCode!==0){
                    wx.showModal({
                    confirmText: '确定',
                    content: '图片内容不合法，请重新选择',
                    showCancel: false
                    })
                    return
                }
                wx.navigateTo({
                    url: "../emotionEdit/emotionEdit?choosedEmoUrl=" + o
                });
            }
        });
    },
    chooseEmotion: function(t) {
        var o = this.data.imageList[t.currentTarget.dataset.idx].meta.thumbnail;
        wx.showLoading({
            title: "获取图片"
        }), wx.downloadFile({
            url: o,
            success: function(t) {
                wx.hideLoading(), wx.navigateTo({
                    url: "../emotionEdit/emotionEdit?choosedEmoUrl=" + t.tempFilePath
                });
            },
            fail: function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "连接出错，请稍后重试",
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    onShareAppMessage: function() {
        return {
            title: "表情包制作-图片编辑助手",
            path: "pages/emotionChoose/emotionChoose"
        };
    }
});